<template src="./template.html"></template>

<style src="./style.css" scoped></style>

<script>
    import cartImg from 'assets/images/cart-icon.png';
    import api from 'api/api';
    import modal from 'utils/modal';
    import navigator from 'utils/navigator';
    import route from 'constants/route';
    import constant from 'constants/constant';
    import cartService from 'services/cartService';
    import storage from 'utils/storage';

    const globalEvent = weex.requireModule('globalEvent');

    function getContextPath() {
        var url;
        var bundleUrl = weex.config.bundleUrl;
        url = bundleUrl.split('/').slice(0, -1).join('/');
        return url;
    }
    export default{
        data(){
            return {
                listLoaded: false,
                cartImg: cartImg,
                canBuy: true, //是否可以购买
                cartInfo: {}, //购物车信息对象
                goodsId: null, //商品ID
                tabItems: [
                    {
                        index: 0,
                        title: '商品',
                        fontSize: 36,
                        selected: true,
                        src: `${getContextPath()}` + '/index.js',
                        visibility: 'hidden'
                    },
                    {
                        index: 1,
                        title: '详情',
                        fontSize: 36,
                        selected: false,
                        src: `${getContextPath()}` + '/info.js',
                        visibility: 'hidden'
                    },
                    {
                        index: 2,
                        title: '评价',
                        fontSize: 36,
                        selected: false,
                        src: `${getContextPath()}` + '/evaluate.js',
                        visibility: 'hidden'
                    }
                ]
            }
        },
        created(){
            let self = this;
            let config = weex.config;
            if(typeof config.id === 'undefined') {
                modal.toast('该商品不存在');
                return false;
            }
            self.goodsId = config.id;
            self.getGoodsDetail(res =>{
                self.setGoodsId(self.goodsId);
                self.listLoaded = true;
            });

            //监听购物车通用事件
            cartService.cartAddEventListener(res=>{
                self.cartInfo = res;
            });

            //监听tab切换事件
            globalEvent.addEventListener(constant.EVENT_TAB_CHANGE, function (e) {
                self.$refs.fTab.select(e.data);
            });

        },
        methods: {
            getGoodsDetail(callback){
                let self = this;
                api.sendHttp('detailIndexApi', {
                    id: self.goodsId
                },false).then(res => {
                    self.vendorData(res.data);
                    if(typeof callback == 'function') callback(res);
                }, res => {
                    modal.toast(res.msg || "获取商品详情失败");
                });
            },
            vendorData(data){
                let self = this;
                let status = data.sent_status;
                if(status != '有货'){
                    self.canBuy = false;
                }

                //获取本地缓存下的购物车数量
                storage.getItem('cartInfo').then(res=>{
                     res = res || {};
                    if(Array.prototype.toString.call(res) == "[object String]")  res = JSON.parse(res);
                    self.cartInfo = res;
                });
            },
            setGoodsId(id){
                let self = this;
                self.tabItems.map((v,i,arr)=>{
                    v.src += "?goodsId=" + id;
                });
            },
            btnClick(){
                let self = this;
                //存储本地购物车数量并发送加入购物车成功事件
                api.sendHttp('addCartApi', {
                    goods: self.goodsId + '_' + 1 + '_' + 1
                }).then(res => {
                    modal.toast("加入购物车成功");
                    cartService.sendEvent(res.data,res.data.goods);
                }, res => {
                    modal.toast(res.msg || '加入购物车失败');
                });
            },
             /*去购物车*/
            goToCart(){
                let options = {
                    classType: constant.Pop_Class_Type,
                    url: route.cart,
                    title: '购物车'
                }
                navigator.push(options);
            }
        },
        destroyed(){
            globalEvent.removeEventListener(constant.EVENT_TAB_CHANGE);
            cartService.removeCartAddEventListener();
        }
    }
</script>
